package com.cinsea.ext.cjspx;

import com.cinsea.common.ActionType;
import com.cinsea.common.BasePX;
import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.exception.DataOperationException;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.SqlUtil;
import com.cinsea.util.StringUtil;
import com.cinsea.workflow.model.Wfnode;

import java.util.List;
import java.util.Map;

/**
 * User: xf
 * Date: 12-9-24
 * Time: 下午3:16
 */
public class MeetingApplyPX extends BasePX{
    @Override
    public void after(Map context) {
        JdbcDao jdbcDao= Env.getBean("jdbcDao");
        String actiontype= StringUtil.null2String(context.get("actiontype"));
        Wfnode node=(Wfnode)context.get("wfnode");
        String processid=(String)context.get("processid");
        if((actiontype.equals(ActionType.SUBMIT_WFPROCESS)||actiontype.equals(ActionType.SAVE_WFPROCESS))&&node!=null&&node.getId().equals("402881e43616a5f2013616cfb2e80002")){//申请节点提交或保存时判断
            List<Map> shuliangs=jdbcDao.queryForList("select meetingroom,jieshuriqi,meetingtime,jiesushijian from ut_meetingapp where processid=?", new Object[]{processid});
            Map obj=shuliangs.get(0);
            String meetingroom=StringUtil.null2String(obj.get("meetingroom"));
            String jieshuriqi=StringUtil.null2String(obj.get("jieshuriqi"));
            String meetingtime=StringUtil.null2String(obj.get("meetingtime"));
            String jiesushijian=StringUtil.null2String(obj.get("jiesushijian"));
            boolean flag=checkSchedule(processid,meetingroom,jieshuriqi,meetingtime,jieshuriqi,jiesushijian);
            if(!flag) throw new DataOperationException("会议室被占用。");
        }
    }

    private boolean checkSchedule(String processid, String resourceid,String startdatestr, String starttimestr, String enddatestr, String endtimestr) {
            processid=StringUtil.null2String(processid);
        String tablename="ut_meetingapp";
        String resources="meetingroom";
            String begindate="jieshuriqi";
            String begintime="meetingtime";
        String enddate="jieshuriqi";
        String endtime="jiesushijian";
            String sql1 = "select * from "+tablename+" where " + SqlUtil.concat(begindate, "' '", begintime) + " <= ? " + " and " +  SqlUtil.concat(enddate,"' '",endtime) + ">? and "+resources+"=? and processid!=? and deleted=0";
            String sql2 = "select * from " + tablename + " where " + SqlUtil.concat(begindate,"' '",begintime) + " < ? " + " and " +  SqlUtil.concat(enddate,"' '",endtime) + ">=? and "+resources+"=? and processid!=? and deleted=0";
            String sql3 = "select * from " + tablename + " where " + SqlUtil.concat(begindate,"' '",begintime) + " > ? " + " and " +  SqlUtil.concat(enddate,"' '",endtime) + "<? and "+resources+"=? and processid!=? and deleted=0";
        JdbcDao jdbcDao= Env.getBean("jdbcDao");
            List list1 = jdbcDao.queryForList(sql1.toLowerCase(), new Object[]{startdatestr+" "+ starttimestr, startdatestr+" "+starttimestr,resourceid,processid});
            if (list1.size() > 0) return false;
            List list2 = jdbcDao.queryForList(sql2.toLowerCase(), new Object[]{enddatestr+" "+ endtimestr, enddatestr+" "+  endtimestr,resourceid,processid});
            if (list2.size() > 0) return false;
            List list3 = jdbcDao.queryForList(sql3.toLowerCase(), new Object[]{startdatestr+" "+  starttimestr, enddatestr+" "+  endtimestr,resourceid,processid});
            if (list3.size() > 0) return false;
            return true;
        }
}
